package com.example.gobang_system.common.pojo;

import com.example.gobang_system.common.pojo.UserInfo;

import lombok.Data;
import java.util.UUID;

@Data
public class Room {
    private static final int MAX_ROW = 15;
    private static final int MAX_COL = 15;
    /**
     * 房间 id
     */
    private String roomId;
    /**
     * 玩家1
     */
    private UserInfo user1;
    /**
     * 玩家2
     */
    private UserInfo user2;
    /**
     * 先手玩家 id
     */
    private Long whiteUserId;
    /**
     * 落子数量
     */
    private int piecesNumber;
    /**
     * 棋盘
     */
    private int[][] board = new int[MAX_ROW][MAX_COL];

    public Room() {
        // 使用 uuid 作为房间唯一标识
        roomId = UUID.randomUUID().toString().replace("-", "") + System.currentTimeMillis();
    }


    /**
     * 判断当前位置是否已经有子
     * @param row
     * @param col
     * @return
     */
    public boolean isEmpty(int row, int col) {
        return board[row][col] == 0;
    }

    /**
     * 在当前位置落子
     * @param row
     * @param col
     * @param chess
     */
    public void putChess(int row, int col, int chess) {
        board[row][col] = chess;
        piecesNumber++;
    }

    /**
     * 打印棋盘
     */
    public void printBoard() {
        System.out.println("------------------棋盘-----------------------");
        for (int i = 0; i < MAX_ROW; i++) {
            for (int j = 0; j < MAX_COL; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("------------------棋盘-----------------------");
    }

    /**
     * 判断胜负
     * @param row
     * @param col
     * @param chess
     * @return 0：尚未分出胜负 -1：平局
     */
    public Long checkWinner(int row, int col, int chess) {
        // 行
        for (int c = col - 4; c <= col; c++) {
            try {
                if(board[row][c] == chess
                        && board[row][c + 1] == chess
                        && board[row][c + 2] == chess
                        && board[row][c + 3] == chess
                        && board[row][c + 4] == chess) {
                    return chess == user1.getUserId() ?
                            user1.getUserId() : user2.getUserId();
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                continue;
            }
        }
        // 列
        for (int r = row - 4; r <= row; r++) {
            try {
                if(board[r][col] == chess
                        && board[r + 1][col] == chess
                        && board[r + 2][col] == chess
                        && board[r + 3][col] == chess
                        && board[r + 4][col] == chess) {
                    return chess == 1 ?
                            user1.getUserId() : user2.getUserId();
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                // 若出现数据下标越界的情况，则继续判断
                continue;
            }
        }
        // 左对角线
        for (int r = row - 4, c = col - 4; r <= row && c <= col; r++, c++) {
            try {
                if(board[r][c] == chess
                        && board[r + 1][c + 1] == chess
                        && board[r + 2][c + 2] == chess
                        && board[r + 3][c + 3] == chess
                        && board[r + 4][c + 4] == chess) {
                    return chess == 1 ? user1.getUserId() : user2.getUserId();
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                continue;
            }
        }
        // 右对角线
        for (int r = row - 4, c = col + 4; r <= row && c >= col; r++, c--) {
            try {
                if(board[r][c] == chess
                        && board[r + 1][c - 1] == chess
                        && board[r + 2][c - 2] == chess
                        && board[r + 3][c - 3] == chess
                        && board[r + 4][c - 4] == chess) {
                    return chess == 1 ? user1.getUserId() : user2.getUserId();
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                continue;
            }
        }
        // 平局
        if(piecesNumber == MAX_ROW * MAX_COL) {
            return -1L;
        }
        piecesNumber++;
        return 0L;
    }
}
